package com.qf.controller;

import com.qf.response.ResponseData;
import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;
import com.qf.utils.JsonUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 后台接口地址:控制器
 */
@WebServlet("/check")
public class CheckUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1)获取前台用户提交的参数
        String username = request.getParameter("username");
        //2)调用业务接口
        UserService userService= new UserServiceImpl();
        boolean flag = userService.checkUser(username);
        //封装ResponseData实体
        ResponseData responseData = new ResponseData();
        if (flag){
            //成立,可以注册
            responseData.setCode(1);
            responseData.setMsg("恭喜您,可以使用");
        }else {
            //不成立,不可以注册
            responseData.setCode(0);
            responseData.setMsg("×");
        }
        //无论上面成立不成立,最终将当前的responseDate响应json格式过去
        String jsonStr = JsonUtils.object2json(responseData);
        response.getWriter().write(jsonStr);
    }
}
